package com.gper.edu.vip.builder.prototype;

import com.alibaba.fastjson.JSON;
import lombok.Data;

import java.util.List;

/**
 * @Author: ellis.guan
 * @Description: 基于json实现一个原型式的深度克隆
 * @Date: 2020/3/1 20:11
 * @Version: 1.0
 */
@Data
public class JSONPrototype {
    private String name;
    private int age;
    private List<String> hobbies;

    public JSONPrototype(String name, int age, List<String> hobbies) {
        this.name = name;
        this.age = age;
        this.hobbies = hobbies;
    }

    public JSONPrototype clone(){
        return JSON.parseObject(JSON.toJSONString(this),this.getClass());
    }

    public List<String> getHobbies() {
        return hobbies;
    }

    @Override
    public String toString() {
        return "JSONPrototype{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", hobbies=" + hobbies +
                '}';
    }
}
